package com.zzh.ideas.web.framework.handler;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * description: <br>
 * createTime: 2019/12/59:48 <br>
 *
 * @author zzh
 */
@Component
public class ApiArgumentResolver implements HandlerMethodArgumentResolver {

    public ApiArgumentResolver() {
        System.out.println("jalksdjfl");
    }


    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return true;
    }


    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()))){
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = br.readLine()) != null) {
                sb.append(line);
            }
            String str = sb.toString();
            System.out.println(str);
        }
        System.out.println();
        System.out.println(parameter);
        return null;
    }
}
